﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodeBuilder2.DAL.Connection;
using CodeBuilder2.Model;
using System.Data;
using CodeBuilder2.DAL.Schema;

namespace CodeBuilder2.BLL.Schema
{
    class DataBaseBLL
    {
        DataBaseDAL dbDal = new DataBaseDAL();

        /// <summary>
        /// 获取指定服务器上指定数据库的全部表名称，如果没有则为 null
        /// </summary>
        /// <param name="serverName">指定的服务器名称</param>
        /// <param name="dbName">指定的数据库名称</param>
        /// <returns>全部的表名</returns>
        internal List<string> GetTablesName(string serverName, string dbName)
        {
            ConnectionDAL cDal = new ConnectionDAL();
            Dictionary<string, TargetServer> dic = cDal.GetConnectedServers();
            TargetServer ts = dic[serverName];
            DataTable dt = dbDal.GetTables(ts, dbName);

            List<string> tablesName = null;

            if (dt != null&&dt.Rows.Count>0)
            {
                tablesName = new List<string>(dt.Rows.Count);
                int index=dt.Columns.IndexOf("table_name");
                foreach (DataRow dr in dt.Rows)
                {
                    tablesName.Add(dr[index].ToString());
                }
            }

            return tablesName;
        }
    }
}
